const { Menu, ipcMain, BrowserWindow, shell, app } = require("electron");

// 顶部菜单
let topMenuTpl = [
  {
    label: "文件",
    submenu: [
      {
        label: "新建",
        accelerator: "Ctrl+N",
        click: function () {
          BrowserWindow.getFocusedWindow().webContents.send("action", "new");
        },
      },
      {
        label: "打开",
        accelerator: "Ctrl+O",
        click: function () {
          BrowserWindow.getFocusedWindow().webContents.send("action", "open");
        },
      },
      {
        label: "保存",
        accelerator: "Ctrl+S",
        click: function () {
          BrowserWindow.getFocusedWindow().webContents.send("action", "save");
        },
      },
      { type: "separator" },
      {
        label: "打印",
        accelerator: "Ctrl+P",
        click: function () {
          BrowserWindow.getFocusedWindow().webContents.print();
        },
      },
      {
        label: "退出",
        accelerator: "Ctrl+Q",
        click: function () {
          BrowserWindow.getFocusedWindow().webContents.send("action", "exit");
        },
      },
    ],
  },
  {
    label: "编辑",
    submenu: [
      { label: "撤销", role: "undo" },
      { label: "恢复", role: "redo" },
      { type: "separator" },
      { label: "截切", role: "cut" },
      { label: "复制", role: "copy" },
      { label: "粘贴", role: "paste" },
      { label: "删除", role: "delete" },
      { label: "全选", role: "selectall" },
    ],
  },
  {
    label: "视图",
    submenu: [
      { label: "重载", role: "reload" },
      { label: "缩小", role: "zoomout" },
      { label: "放大", role: "zoomin" },
      { label: "重置缩放", role: "resetzoom" },
      { type: "separator" },
      { label: "全屏", role: "togglefullscreen" },
    ],
  },
  {
    label: "帮助",
    submenu: [
      {
        label: "关于",
        click() {
          shell.openExternal("https://www.yuque.com/li_shifeng/glew8t/zmo13g");
        },
      },
    ],
  },
];

let topMenu = Menu.buildFromTemplate(topMenuTpl);
Menu.setApplicationMenu(topMenu);

// 右键菜单
let contextMenuTpl = [
  { label: "撤销", role: "undo" },
  { label: "恢复", role: "redo" },
  { type: "separator" },
  { label: "截切", role: "cut" },
  { label: "复制", role: "copy" },
  { label: "粘贴", role: "paste" },
  { type: "separator" },
  { label: "全选", role: "selectall" },
];

let contextMenu = Menu.buildFromTemplate(contextMenuTpl);

ipcMain.on("contextmenu", () => {
  contextMenu.popup(BrowserWindow.getFocusedWindow());
});

ipcMain.on("exit-app", () => {
  app.quit();
});
